﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NLog;
using System.Web.SessionState;
namespace FPC.Offer.UI.GeneralProcessor.Daily
{
    /// <summary>
    /// Daily_show 的摘要说明
    /// </summary>
    public class Daily_show : IHttpHandler, IReadOnlySessionState
    {
        FPC.Offer.Service.System_DailyService s = new Service.System_DailyService();
        private static Logger logger = LogManager.GetCurrentClassLogger();
        
        public void ProcessRequest(HttpContext context)
        {
          
          context.Response.ContentType = "text/plain";
          var Actioon = context.Request["Actioon"].ToString();
          var UserName = context.Session[FPC.Offer.Common.SystemKeys.COOKIE_USER_NAME_REMEMBER];
            var AmilyID =Convert.ToInt32( context.Session[FPC.Offer.Common.SystemKeys.COOKIE_USER_UserID_REMEMBER]);
            //显示
          if(Actioon=="显示")
          {
              string cc = "";

              if (AmilyID != 0 || UserName != null)
              {
                  cc = "操作人警号:" + AmilyID.ToString();
                  cc += ",操作人：" + UserName.ToString() + "查看日志信息";
                  logger.Info(cc);
              }
             
             
            var lis = JsonConvert.SerializeObject(s.Select());
            context.Response.Write(lis);
          }
            //搜索
           if(Actioon=="搜索")
           {
               var time1 =context.Request["time1"].ToString();
               var time2 = context.Request["time2"].ToString();
               var name = context.Request["name"].ToString();
               List<Model.System_Daily> list =s.Select();
               string cc = "";

               if (AmilyID != 0 || UserName!= null)
               {
                   cc = "操作人警号:" + AmilyID.ToString();
                   cc += ",操作人：" + UserName.ToString() + "搜索日志信息";
                   logger.Info(cc);
               }

               
               if(time1!=null&&time1!="")
               {
                   list = list.Where(m => m.SystemDailyDate >=Convert.ToDateTime(time1)).ToList();
               }
               if(time2!=null&&time2!="")
               {
                   list = list.Where(m => m.SystemDailyDate <=Convert.ToDateTime(time2)).ToList();
               }
               if(name!=""&&name!=null)
               {
                   list = list.Where(m => m.SystemContent==name).ToList();
               }
               context.Response.Write(JsonConvert.SerializeObject(list));
           }
           //删除
           if (Actioon == "del")
           {
               string cc = "";

               if (AmilyID != 0 || UserName != null)
               {
                   cc = "操作人警号:" + AmilyID.ToString();
                   cc += ",操作人：" + UserName.ToString() + "删除日志信息";
                   logger.Info(cc);
               }

               
               var SystemDailyId = Convert.ToInt32(context.Request["SystemDailyId"]);
               Model.System_Daily li = s.Select().Where(m => m.SystemDailyId == SystemDailyId).FirstOrDefault();
               if (s.Delete(li) > 0)
               {
                   context.Response.Write(1);
               }
               else
               {
                   context.Response.Write(0);
               }
           }
           ///批删

           if (Actioon == "dels")
           {
               string cc = "";

               if (AmilyID != 0 || UserName != null)
               {
                   cc = "操作人警号:" + AmilyID.ToString();
                   cc += ",操作人：" + UserName.ToString() + "批量删除日志信息";
                   logger.Info(cc);
               }

               
               string SystemDailyId = context.Request["SystemDailyId"];

               SystemDailyId = SystemDailyId.Substring(0, SystemDailyId.Length - 1);


               if (s.da_deletes(SystemDailyId) > 0)
               {
                   context.Response.Write(1);
               }
               else
               {
                   context.Response.Write(0);
               }
           }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}